package com.spdg.ring.datamgr;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import cn.wolf.base.DataManager;
import cn.wolf.tools.MD5;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.R;
import com.spdg.ring.common.Config;
import com.spdg.ring.entity.MenuEntity;
import com.spdg.ring.sqlite.model.Menu;
import com.spdg.ring.sqlite.service.MenuService;
import com.spdg.ring.util.MenuUtil;

/**
 * <p>Title:  鑿滃崟閰嶇疆鏁版嵁绠＄悊绫�</p>
 * <p>Description: 
 * 1銆佹爮鐩暟鎹鐞嗭紙鏍忕洰鍥剧墖锛屾爮鐩悕锛屾爮鐩暟鎹俊婧愶級
 * 2銆�</p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-20             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class MenuDataMgr extends DataManager {

	public static MenuDataMgr mInstance = new MenuDataMgr();

	public static MenuDataMgr getInstance() {
		if (mInstance == null)
			mInstance = new MenuDataMgr();
		return mInstance;
	}

	// 鑿滃崟榛樿鍥剧墖
	public static final int[] drawables = new int[] { R.drawable.icon_menu_1,
			R.drawable.icon_menu_2, R.drawable.icon_menu_3, R.drawable.icon_menu_4,
			R.drawable.icon_menu_5 };

	// 鑿滃崟榛樿鍚嶇О
	public static final int[] names = new int[] { R.string.menu_name_1, R.string.menu_name_2,
			R.string.menu_name_3, R.string.menu_name_4, R.string.menu_name_5 };

	private String aboutUrl; // 鍏充簬鍦板潃

	private List<Menu> menus = new ArrayList<Menu>(4);
	private boolean LOAD_SUCCESS = false; // true缂撳瓨鍔犺浇鎴愬姛銆傚綋绋嬪簭寮傚父锛岃鍊间负false

	/**
	 * 浠庢暟鎹簱鍔犺浇鑿滃崟鍒板唴瀛�
	 * @param context
	 */
	public void loadDataSqlite(Context context) {
		MenuService service = MenuService.newInstance(context);
		List<Menu> list = service.findAll();
		if (list != null && list.size() > 0) {
			refresh(context, list);
			LOAD_SUCCESS = true;
		}
	}

	/**
	 * 淇濆瓨鑿滃崟鍒版暟鎹簱
	 * @param context
	 * @param data
	 */
	public void saveDataSqlite(Context context, List<Menu> data) {
		if (data == null || data.size() == 0)
			return;

		MenuService service = MenuService.newInstance(context);
		for (Menu menu : data) {
			service.saveOrUpdate(menu);
		}
	}

	/**
	 * 鍒锋柊鑿滃崟鏍忕洰鏁版嵁
	 * @param context
	 * @param data 鏍忕洰鏁版嵁
	 */
	public void refresh(Context context, List<Menu> data) {
		if (data == null || data.size() == 0) {
			return;
		}

		synchronized (menus) {
			menus.clear();
			menus.addAll(data);
		}
	}

	/**
	 * 鍒锋柊鑿滃崟涓诲叆鍙�
	 * @param context
	 * @param entity 鑿滃崟鏁版嵁锛屽寘鎷叧浜庡湴鍧�紝搴旂敤鍚嶏紝鏍忕洰閰嶇疆鏁版嵁 
	 */
	public void refresh(Context context, MenuEntity entity) {
		if (entity == null) {
			return;
		}

		MenuUtil.saveAbout(context, entity.getAbout());

		refresh(context, entity.getMenus());
	}

	/**
	 * 鏍规嵁鑿滃崟搴忓垪鑾峰彇鍥剧墖
	 * @param context
	 * @param index
	 * @return
	 */
	public Drawable getMenuIcon(Context context, int index) {
		Drawable drawable = null;
		if (index >= drawables.length)
			return drawable;

		// 鑾峰彇sd鍗＄紦瀛樿彍鍗曞浘鐗�
		Menu menu = getMenu(context, index);
		if (menu != null) {
			drawable = getMenuIconDrawable(menu);
		}

		if (drawable == null) {
			drawable = context.getResources().getDrawable(drawables[index]);
		}
		return drawable;
	}

	/**
	 * 鏍规嵁鑿滃崟搴忓垪鑾峰彇鍥剧墖
	 * @param context
	 * @param index
	 * @return
	 */
	public String getMenuName(Context context, int index) {
		String name = "";

		if (index >= names.length)
			return name;

		Menu menu = getMenu(context, index);
		if (menu != null)
			name = menu.getName();

		if (StringUtil.isEmpty(name)) {
			name = context.getString(names[index]);
		}

		return name;
	}

	public Menu getMenu(Context context, int index) {
		Menu menu = null;

		if (!LOAD_SUCCESS) {
			loadDataSqlite(context);
		}

		if (menus == null || index >= menus.size())
			return menu;

		menu = menus.get(index);
		return menu;
	}

	public List<Menu> getMenus(Context context) {
		if (!LOAD_SUCCESS) {
			loadDataSqlite(context);
		}

		return menus;
	}

	/**
	 * 鑾峰彇鍏充簬鍦板潃
	 * @param context
	 * @return
	 */
	public String getAboutUrl(Context context) {
		if (StringUtil.isEmpty(aboutUrl)) {
			aboutUrl = MenuUtil.getAbout(context);
		}
		return aboutUrl;
	}

	public BitmapDrawable getMenuIconDrawable(Menu menu) {
		if (menu == null || StringUtil.isEmpty(menu.getIcon()))
			return null;

		String cacheKey = MD5.getMD5Str(menu.getIcon());
		Bitmap bitmap = BitmapFactory.decodeFile(Config.SDCARD_MENU_DIR + cacheKey);
		if (bitmap != null) {
			return new BitmapDrawable(bitmap);
		}
		return null;
	}

	public BitmapDrawable getMenuIconFocusDrawable(Menu menu) {
		if (menu == null || StringUtil.isEmpty(menu.getIcon2())) {
			return null;
		}

		String cacheKey = MD5.getMD5Str(menu.getIcon2());
		Bitmap bitmap = BitmapFactory.decodeFile(Config.SDCARD_MENU_DIR + cacheKey);
		if (bitmap != null) {
			return new BitmapDrawable(bitmap);
		}
		return null;
	}

	public BitmapDrawable getMenuMoreIconFocus(Context context) {
		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.icon_menu_5_pressed);
		return new BitmapDrawable(bitmap);
	}

	public void deleteMenuIcon() {
		File file = new File(Config.SDCARD_MENU_DIR);
		if (file != null && file.exists()) {
			File[] files = file.listFiles();
			for (File f : files) {
				f.delete();
			}
		}
	}

}
